import Vue from 'vue'
import Router from 'vue-router'
/* Layout */
import Layout from '@/layout'

Vue.use(Router)

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  }
  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]
export const asyncRoutes = [
  {
    path: '/business',
    component: Layout,
    redirect: '/business/home',
    name: 'Business',
    alwaysShow: true,
    meta: {
      title: '经营看板',
      icon: 'el-icon-s-help'
    },
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/business/home/index.vue'),
        meta: {
          title: '首页',
          icon: 'table',
          roles: ['admin', 'warehouse']
        }
      },
      {
        path: 'financial',
        name: 'Financial',
        component: () => import('@/views/business/financial/index'),
        meta: {
          title: '财务支出',
          icon: 'table',
          roles: ['admin', 'warehouse']
        }
      },
      {
        path: 'inventory',
        name: 'Inventory',
        component: () => import('@/views/business/inventory/index'),
        meta: {
          title: '库存统计',
          icon: 'tree',
          roles: ['admin', 'warehouse']
        }
      },
      {
        path: 'operation',
        name: 'Operation',
        component: () => import('@/views/business/operation/index'),
        meta: {
          title: '仓库经营',
          icon: 'tree',
          roles: ['admin', 'warehouse']
        }
      }
    ]
  },
  {
    path: '/basic',
    component: Layout,
    redirect: '/basic/product/productInfo',
    alwaysShow: true,
    name: 'Basic',
    meta: {
      title: '基础数据',
      icon: 'nested'
    },
    children: [{
      path: 'product',
      component: () => import('@/views/basic/product/index'), // Parent router-view
      name: 'Product',
      meta: {
        title: '产品',
        roles: ['admin', 'warehouse']
      },
      children: [{
        path: 'productInfo',
        component: () => import('@/views/basic/product/info/index'),
        name: 'ProductInfo',
        meta: {
          title: '产品信息'
        }
      },
      {
        path: 'productPrice',
        component: () => import('@/views/basic/product/price/index'),
        name: 'ProductPrice',
        meta: {
          title: '价格'
        }
      },
      {
        path: 'productClassification',
        component: () => import('@/views/basic/product/classification/index'),
        name: 'ProductClassification',
        meta: {
          title: '分类'
        }
      }
      ]
    },
    {
      path: 'warehouse',
      component: () => import('@/views/basic/warehouse/index'),
      name: 'Warehouse',
      meta: {
        title: '仓库看板',
        roles: ['admin', 'warehouse']
      }
    }
    ]
  },
  {
    path: '/customer',
    component: Layout,
    redirect: '/customer/customerManage',
    name: 'Customer',
    alwaysShow: true,
    meta: {
      title: '客户看板',
      icon: 'el-icon-s-help',
      roles: ['admin']
    },
    children: [
      {
        path: 'customerManage',
        name: 'CustomerManage',
        component: () => import('@/views/customer/manage/index'),
        meta: {
          title: '客户管理',
          icon: 'table',
          roles: ['admin']
        }
      },
      {
        path: 'customerAnalusis',
        name: 'CustomerAnalusis',
        component: () => import('@/views/customer/analysis/index'),
        meta: {
          title: '客户分析',
          icon: 'table',
          roles: ['admin']
        }
      }
    ]
  },
  {
    path: '/sales',
    component: Layout,
    redirect: '/sales/salesOrder',
    name: 'Sales',
    alwaysShow: true,
    meta: {
      title: '销售看板',
      icon: 'el-icon-s-help',
      roles: ['admin']
    },
    children: [
      {
        path: 'salesOrder',
        name: 'SalesOrder',
        component: () => import('@/views/sales/order/index'),
        meta: {
          title: '销售订单',
          icon: 'table',
          roles: ['admin']
        }
      },
      {
        path: 'salesStatistics',
        name: 'SalesStatistics',
        component: () => import('@/views/sales/statistics/index'),
        meta: {
          title: '销售统计',
          icon: 'table',
          roles: ['admin']
        }
      }
    ]
  },
  {
    path: '/purchase',
    component: Layout,
    redirect: '/purchase/supplier',
    alwaysShow: true,
    name: 'Purchase',
    meta: {
      title: '采购看板',
      icon: 'nested',
      roles: ['admin']
    },
    children: [{
      path: 'supplier',
      component: () => import('@/views/purchase/supplier/index'), // Parent router-view
      name: 'Supplier',
      meta: {
        title: '供应商看板',
        roles: ['admin']
      },
      children: [
        {
          path: 'supplierInfo',
          component: () => import('@/views/purchase/supplier/info/index'),
          name: 'SupplierInfo',
          meta: {
            title: '供应商信息'
          }
        },
        {
          path: 'supplierPrice',
          component: () => import('@/views/purchase/supplier/price/index'),
          name: 'SupplierPrice',
          meta: {
            title: '供应商价格'
          }
        }
      ]
    },
    {
      path: 'supplierOrder',
      component: () => import('@/views/purchase/order/index'),
      name: 'SupplierOrder',
      meta: {
        title: '采购订单',
        roles: ['admin']
      }
    },
    {
      path: 'supplierStatistics',
      component: () => import('@/views/purchase/statistics/index'),
      name: 'SupplierStatistics',
      meta: {
        title: '订单统计',
        roles: ['admin']
      }
    },
    {
      path: 'procurement',
      component: () => import('@/views/purchase/procurement/index'),
      name: 'Procurement',
      meta: {
        title: '采购统计',
        roles: ['admin']
      }
    }
    ]
  },
  {
    path: '/stock',
    component: Layout,
    redirect: '/stock/stockInfo',
    name: 'Stock',
    alwaysShow: true,
    meta: {
      title: '库存看板',
      icon: 'el-icon-s-help',
      roles: ['admin', 'warehouse']
    },
    children: [
      {
        path: 'inbound',
        name: 'Inbound',
        component: () => import('@/views/stock/inbound/index'),
        meta: {
          title: '入库',
          icon: 'table',
          roles: ['admin', 'warehouse']
        }
      },
      {
        path: 'outBound',
        name: 'OutBound',
        component: () => import('@/views/stock/outbound/index'),
        meta: {
          title: '出库',
          icon: 'table',
          roles: ['admin', 'warehouse']
        }
      },
      {
        path: 'stockInfo',
        name: 'StockInfo',
        component: () => import('@/views/stock/info/index'),
        meta: {
          title: '库存信息',
          icon: 'table',
          roles: ['admin', 'warehouse']
        }
      },
      {
        path: 'stockStatistics',
        name: 'StockStatistics',
        component: () => import('@/views/stock/statistics/index'),
        meta: {
          title: '库存统计',
          icon: 'table',
          roles: ['admin', 'warehouse']
        }
      }
    ]
  },

  // 如果需要配置重定向404页面的话，需要配置在asyncRoutes的最后
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
]

const createRouter = () => new Router({
  mode: 'hash', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
